package cn.group.controller;

import cn.group.domain.Room;
import cn.group.service.RoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @RequestMapping("/showRoom")
     public String getRommList(){
        //直接返回jsp页面
        return "room";
    }
    @RequestMapping("/getroom")
    @ResponseBody
    public Map<String,Object>getRoom(){
        List<Room>rooms = roomService.getAll();
        System.out.println(rooms);
        Map<String,Object>result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取教室信息成功！");
        result.put("roomList",rooms);
        return result;
    }
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String,Object>getone(Integer id){
        Room room = roomService.getOneById(id);
        System.out.println(room);
        Map<String,Object>result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取教室信息成功！");
        result.put("roomList",room);
        return result;
    }
    @RequestMapping("/getListBypage")
    @ResponseBody
    public Map<String,Object>getListBypage(Integer currentPage,Integer pageSize){
        PageInfo<Room> pageInfo = roomService.getRoomByPage(currentPage,pageSize);
        Map<String,Object>result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("roomList",pageInfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }
    @RequestMapping("/edit")//添加教室位置
    @ResponseBody
    public Map<String,Object>edit(Room room){
        Map<String,Object>result = new HashMap<>();
        boolean modified;
        if (room.getId()> 0){
            modified = roomService.modifyOne(room);
        }else {
            modified = roomService.addOne(room);
        }
        if (!modified){
            result.put("code",-1);
            result.put("message","编辑教室数据失败");
        }else {
            result.put("code",0);
            result.put("message","添加教室数据成功");
        }
        return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object>delete(@RequestParam("id") Integer id){
        Map<String,Object> result = new HashMap<>();
        boolean deleted = roomService.removeOne(id);
        if (!deleted){
            result.put("code",-1);
            result.put("message","删除用户数据失败");
        }else {
            result.put("code",0);
            result.put("message","删除用户数据成功");
        }
        return result;
    }
}
